Public Class frmTheoDoiXuatHangHoa
    Dim dataPhieuXuat As DataTable
    Dim dataChiTietPhieuXuat As DataTable

    Dim sqlQuery As String = ""

    '***********************************
    Private Sub loadPhieuXuat(ByVal tuNgay As Date, ByVal denNgay As Date)
        sqlQuery = "SELECT xhh.ma_chungtu, xhh.ngay_chungtu, xhh.so_hoadon, xhh.ngay_hoadon, kh.ten_nhacungcap, k.ten_kho, xhh.diengiai "
        sqlQuery += "FROM tbl_xuat_hanghoa as xhh, tbl_khachhang as kh, tbl_kho as k "
        sqlQuery += "WHERE xhh.ma_khachhang = kh.ma_nhacungcap "
        'sqlQuery += "WHERE xhh.ma_phu = kh.ma_nhacungcap "
        sqlQuery += "AND xhh.ma_kho = k.ma_kho "
        sqlQuery += "AND xhh.ngay_chungtu BETWEEN '" & Format(tuNgay, "yyyy-MM-dd") & "' AND '" & Format(denNgay, "yyyy-MM-dd") & "' "
        sqlQuery += "ORDER BY xhh.ngay_chungtu DESC"
        dataPhieuXuat = moduleDatabaseConn.GetDataTable(sqlQuery)
        Me.gridPhieuXuat.DataSource = dataPhieuXuat
        Me.lblTotal.Text = dataPhieuXuat.Rows.Count
    End Sub

    Private Sub loadChiTietPhieuXuat(ByVal ma_chungtu As String)
        sqlQuery = "SELECT xcthh.ma_hanghoa, hh.ten_bietduoc, hh.donvi_banchan, xcthh.solo, xcthh.handung, xcthh.soluongle as soluongle "
        sqlQuery += "FROM tbl_xuat_chitiet_hanghoa as xcthh, tbl_hanghoa as hh, tbl_xuat_hanghoa as xhh "
        sqlQuery += "WHERE xcthh.ma_chungtu = xhh.ma_chungtu "
        sqlQuery += "AND xcthh.ma_hanghoa = hh.ma_hanghoa "
        sqlQuery += "AND xcthh.ma_chungtu = '" & ma_chungtu & "' "
        'sqlQuery += "GROUP BY xcthh.ma_hanghoa, xcthh.solo "
        sqlQuery += "ORDER BY hh.ten_bietduoc"
        dataChiTietPhieuXuat = moduleDatabaseConn.GetDataTable(sqlQuery)
        Me.gridChiTietPhieuXuat.DataSource = dataChiTietPhieuXuat
        Me.lblTotalChiTiet.Text = dataChiTietPhieuXuat.Rows.Count

        sqlQuery = "SELECT thanhtien, tienvat, tongtien FROM tbl_xuat_hanghoa WHERE ma_chungtu = '" & ma_chungtu & "'"
        Dim data_temp As DataTable = moduleDatabaseConn.GetDataTable(sqlQuery)
        Try
            Me.lblThanhTien.Text = Format(CDec(data_temp.Rows.Item(0).Item(0)), "N0")
            Me.lblTongTien.Text = Format(CDec(data_temp.Rows.Item(0).Item(2)), "N0")
        Catch ex As Exception

        End Try
    End Sub
    '***********************************

    Private Sub frmTheoDoiXuatHangHoa_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        moduleUtils.setDinhDangVietNam()
        Me.txtTuNgay.Text = moduleUtils.getNgayDauThang(Today.Year, Today.Month)
        Me.txtDenNgay.Text = Today
        Me.btnDanhSach.Focus()
    End Sub

    Private Sub btnDanhSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDanhSach.Click
        loadPhieuXuat(CDate(Me.txtTuNgay.Text.Trim), CDate(Me.txtDenNgay.Text.Trim))
    End Sub

    Private Sub gridPhieuXuat_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridPhieuXuat.CellClick
        If e.RowIndex >= 0 Then
            loadChiTietPhieuXuat(Me.gridPhieuXuat.Item(0, e.RowIndex).Value)
        End If
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub DPTDenNgay_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DPTDenNgay.ValueChanged
        Me.txtDenNgay.Text = Me.DPTDenNgay.Value
    End Sub
    Private Sub DPTTuNgay_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DPTTuNgay.ValueChanged
        Me.txtTuNgay.Text = Me.DPTTuNgay.Value
    End Sub

    Private Sub gridPhieuXuat_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridPhieuXuat.CellDoubleClick
        If e.RowIndex >= 0 Then
            Dim ma_phieuXuat As String = Me.gridPhieuXuat.Item(0, gridPhieuXuat.CurrentCell.RowIndex).Value
            Dim ngay_Xuat As Date = CDate(Me.gridPhieuXuat.Item(1, gridPhieuXuat.CurrentCell.RowIndex).Value)

            Dim isLoaded As Boolean = False
            Dim frmXuat As New frmPhieuXuatHangHoa
            For Each frm As Form In Me.MdiParent.MdiChildren
                If frm.Name = "frmPhieuXuatHangHoa" Then
                    isLoaded = True
                    frmXuat = frm
                    Exit For
                End If
            Next

            If isLoaded Then
                frmXuat.WindowState = FormWindowState.Normal
                Me.WindowState = FormWindowState.Minimized
                frmXuat.Show()
                frmXuat.formLoad(ma_phieuXuat, ngay_Xuat)
            Else
                frmXuat.setLoadForm(0, ma_phieuXuat, ngay_Xuat)
                frmXuat.MdiParent = Me.MdiParent
                frmXuat.Show()
                Me.WindowState = FormWindowState.Minimized
            End If
        End If
    End Sub
End Class